package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.CatalogPropDto;
import com.shop2cn.iapi.agentproducts.parameter.SaveCatalogPropertyBean;
import com.shop2cn.iapi.agentproducts.service.SaveCatalogPropertyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*保存规格属性
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SaveCatalogProperty{
	private static SaveCatalogPropertyBean savecatalogpropertyBean;
	private static SaveCatalogPropertyCall savecatalogpropertyCall;
	private static int sellerId = AgentUsers.rootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("保存规格属性");
	}

	@Before
	public void caseUp() {
		savecatalogpropertyBean=new SaveCatalogPropertyBean();
		savecatalogpropertyCall=new SaveCatalogPropertyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(savecatalogpropertyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(SaveCatalogPropertyBean saveBean){
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		saveBean.getPropertyList().forEach(p->{
			Map map = ymtProductsIWapper.selectUserCatalogProperty(p.getName(),null).get(0);
			Logger.verifyEquals(saveBean.getUserId(),Integer.parseInt(map.get("UserId").toString()),"验证userId");
			Logger.verifyEquals(p.getName(),map.get("PropertyName").toString(),"验证PropertyName");
			Logger.verifyEquals(p.getSubType(),map.get("SubType").toString(),"验证SubType");
			Logger.verifyEquals(p.getValue(),map.get("PropertyValue").toString(),"验证PropertyValue");
		});
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogProperty_01() {
		Logger.start(true,"保存规格属性");
		try {
			savecatalogpropertyBean.setUserId(sellerId);
			List<CatalogPropDto> catalogPropertyDtoList = new ArrayList<>();
			CatalogPropDto catalogPropDto1 = new CatalogPropDto();
			catalogPropDto1.setName("自动化测试1_"+ TestDataManager.getRandomNum(5));
			catalogPropDto1.setPic("http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg");
			catalogPropDto1.setSubType("欧码");
			catalogPropDto1.setValue("170");
			catalogPropertyDtoList.add(catalogPropDto1);

			savecatalogpropertyBean.setPropertyList(catalogPropertyDtoList);
			savecatalogpropertyCall.setData(savecatalogpropertyBean);
			savecatalogpropertyCall.callService();			
			Logger.verifyEquals("200",savecatalogpropertyCall.getString("code"),"验证code");
			verifyDB(savecatalogpropertyBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogProperty_02() {
		Logger.start(true,"保存规格属性,用户id不正确，保存失败");
		try {
			savecatalogpropertyBean.setUserId(0);
			List<CatalogPropDto> catalogPropertyDtoList = new ArrayList<>();
			CatalogPropDto catalogPropDto1 = new CatalogPropDto();
			catalogPropDto1.setName("自动化测试1_"+ TestDataManager.getRandomNum(5));
			catalogPropDto1.setPic("http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg");
			catalogPropDto1.setSubType("欧码");
			catalogPropDto1.setValue("170");
			catalogPropertyDtoList.add(catalogPropDto1);

			savecatalogpropertyBean.setPropertyList(catalogPropertyDtoList);
			savecatalogpropertyCall.setData(savecatalogpropertyBean);
			savecatalogpropertyCall.callService();
			Logger.verifyEquals("400",savecatalogpropertyCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogProperty_03() {
		Logger.start(true,"保存规格属性,规格分类超过2个，保存失败");
		try {
			savecatalogpropertyBean.setUserId(sellerId);
			List<CatalogPropDto> catalogPropertyDtoList = new ArrayList<>();
			CatalogPropDto catalogPropDto1 = new CatalogPropDto();
			catalogPropDto1.setName("自动化测试1_"+ TestDataManager.getRandomNum(5));
			catalogPropDto1.setPic("http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg");
			catalogPropDto1.setSubType("欧码");
			catalogPropDto1.setValue("170");
			catalogPropertyDtoList.add(catalogPropDto1);

			CatalogPropDto catalogPropDto2 = new CatalogPropDto();
			catalogPropDto2.setName("自动化测试2_"+ TestDataManager.getRandomNum(5));
			catalogPropDto2.setPic("http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg");
			catalogPropDto2.setSubType("欧码");
			catalogPropDto2.setValue("170");
			catalogPropertyDtoList.add(catalogPropDto2);

			CatalogPropDto catalogPropDto3 = new CatalogPropDto();
			catalogPropDto3.setName("自动化测试3_"+ TestDataManager.getRandomNum(5));
			catalogPropDto3.setPic("http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg");
			catalogPropDto3.setSubType("欧码");
			catalogPropDto3.setValue("170");
			catalogPropertyDtoList.add(catalogPropDto3);

			savecatalogpropertyBean.setPropertyList(catalogPropertyDtoList);
			savecatalogpropertyCall.setData(savecatalogpropertyBean);
			savecatalogpropertyCall.callService();
			Logger.verifyEquals("201",savecatalogpropertyCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogProperty_04() {
		Logger.start(true,"保存规格属性,规格属性列表为空，保存失败");
		try {
			savecatalogpropertyBean.setUserId(sellerId);
			List<CatalogPropDto> catalogPropertyDtoList = new ArrayList<>();

			savecatalogpropertyBean.setPropertyList(catalogPropertyDtoList);
			savecatalogpropertyCall.setData(savecatalogpropertyBean);
			savecatalogpropertyCall.callService();
			Logger.verifyEquals("400",savecatalogpropertyCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}